public class Solution1335 {
    public int minDifficulty(int[] jobDifficulty, int d) {
        if (d>jobDifficulty.length){
            return -1;
        }
        int[][] f=new int[jobDifficulty.length][d+1];
        for (int i=0;i<jobDifficulty.length;i++){
            int t=Integer.MIN_VALUE;
            for (int j=0;j<=i;j++){
                t=Math.max(t,jobDifficulty[j]);
            }
            f[i][1]=t;
            for (int j=2;j<=Math.min(d,i+1);j++){
                int max=jobDifficulty[i];
                f[i][j]=Integer.MAX_VALUE;
                for (int k=i-1;k>=j-2;k--){
                    f[i][j]=Math.min(f[i][j],f[k][j-1]+max);
                    max=Math.max(max,jobDifficulty[k]);
                }
            }
        }
        return f[jobDifficulty.length-1][d];
    }

    public static void main(String[] args) {
        System.out.println(new Solution1335().minDifficulty(new int[]{11,111,22,222,33,333,44,444},6));
    }
}
